﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Domain;
using Cms.Web.Framework.Controls;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class AdministratorGroups : AdminPage, IAdministratorGroupsView
    {
        private AdministratorGroupsPresenter _presenter;

        #region IAdministratorGroupsView Members

        public void SetGroups(IList<AdministratorGroup> groups)
        {
            rpGroups.DataSource = groups;
            rpGroups.DataBind();
        }

        public string ContextTranslationFile
        {
            get { return "Admin/Translations/AdministratorGroups.xml"; }
        }

        public void SetReplacements(IList<AdministratorGroup> groups, string name)
        {
            ddlReplacements.Items.Clear();
            foreach (AdministratorGroup group in groups)
            {
                ddlReplacements.Items.Add(new ListItem(group.Name, group.ID.ToString()));
            }
            ddlReplacements.Items.Insert(0, new ListItem("- " + GetText(this, "SelectReplacement") + " -", "-1"));
            litReplacementMessage.Text = GetText(this, "ReplacementMessage").Replace("[[Name]]", name);
            litReplacementLabel.Text = GetText(this, "ReplacementLabel").Replace("[[Name]]", name);
            upnlReplacements.Update();
            string script =
                "cms.Dialogs.Confirm(replaceGroup, 'Info', null, 'OK', 'Cancel', true, null, 'replaceGroup');";
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "replaceGroup", script, true);
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new AdministratorGroupsPresenter(this);
            HeadingText = GetText(this, "Heading");
            if (!Page.IsPostBack)
            {
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
            lbDelete.OnClientClick = "return cms.Dialogs.ConfirmLinkButton(this, " +
                                     "function (status) { " +
                                     "if (status == 'ok') " +
                                     "cms.Lists.ShowPaneOverlay('groupPaneOverlay');" +
                                     "else " +
                                     "cms.Lists.HidePaneOverlays(); " +
                                     "}, " +
                                     "'" + GetText(this, "DeleteHeader") + "', " +
                                     "'" + GetText(this, "DeleteMessage") + "', " +
                                     "'" + GetText(this, "DeleteOK") + "', " +
                                     "'" + GetText(this, "DeleteCancel") + "', " +
                                     "true, " +
                                     "cms.Lists.CurrentName);";
        }

        protected void rpGroups_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem)
            {
                return;
            }
            var group = (AdministratorGroup)e.Item.DataItem;
            var litID = (Literal)e.Item.FindControl("litID");
            var litName = (Literal)e.Item.FindControl("litName");
            var litAdministators = (Literal)e.Item.FindControl("litAdministrators");
            litID.Text = "<span id=\"G" + group.ID + "\">";
            litName.Text = group.Name;
            foreach (Administrator admin in group.Administrators)
            {
                if (admin.Deleted)
                {
                    continue;
                }
                if (litAdministators.Text != string.Empty)
                {
                    litAdministators.Text += ", ";
                }
                litAdministators.Text += admin.Fullname ?? admin.Username;
            }
        }
        
        protected void lbDelete_Click(object sender, EventArgs e)
        {
            _presenter.DeleteGroup(int.Parse(hidDeleteID.Value.Substring(1)));
            upnlGroups.Update();
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "initList", "initList();", true);
        }

        protected void btnReplace_Click(object sender, EventArgs e)
        {
            if (hidReplacementID.Value != string.Empty)
            {
                _presenter.ReplaceAndDeleteGroup(int.Parse(hidDeleteID.Value.Substring(1)), int.Parse(hidReplacementID.Value));
            }
            ClientScriptProxy.Current.RegisterStartupScript(pnlActions, GetType(), "initList", "initList();", true);
            upnlGroups.Update();
        }
    }
}